package self.Lunastic.HexaSweeper.Window;

import self.Lunastic.HexaSweeper.Manager.GameManager;
import self.Lunastic.HexaSweeper.Manager.StyleOption;
import self.Lunastic.HexaSweeper.Unit.ButtonArrow;
import self.Lunastic.HexaSweeper.Unit.ButtonControl;
import self.Lunastic.HexaSweeper.Unit.LabelBase;

import java.awt.*;


/**
 * 游戏信息窗口
 */
public class WindowControl extends WindowBase{
    // 创建各种零件
    public final LabelBase setSizeText = new LabelBase(30, 40, this.getWidth() - 60, 25);
    public final LabelBase setBomberText = new LabelBase(30, 80, this.getWidth() - 60, 25);
    public final LabelBase setSizeNumber = new LabelBase(240, 40, this.getWidth() - 60, 25);
    public final LabelBase setBomberNumber = new LabelBase(240, 80, this.getWidth() - 60, 25);

    public final ButtonControl buttonReset = new ButtonControl(0, 135, this.getWidth()/2 - 60, 50, "Reset");
    public final ButtonControl buttonExit = new ButtonControl(this.getWidth()/2, 135, this.getWidth()/2 - 60, 50, "Exit");

    public final ButtonArrow setSizeArrowLeft = new ButtonArrow(0, 42, 20, 20, 4);
    public final ButtonArrow setSizeArrowRight = new ButtonArrow(300, 42, 20, 20, 6);

    public final ButtonArrow setBomberArrowLeft = new ButtonArrow(0, 82, 20, 20, 4);
    public final ButtonArrow setBomberArrowRight = new ButtonArrow(300, 82, 20, 20, 6);

    public final LabelBase flagCount = new LabelBase(10, 200, this.getWidth(), 25);
    public final LabelBase timeCount = new LabelBase(10, 240, this.getWidth(), 25);

    public final LabelBase titleText = new LabelBase(0, this.getHeight() - 160, this.getWidth(), 60);
    public final LabelBase versionText = new LabelBase(0, this.getHeight() - 100, this.getWidth(), 25);

    public WindowControl(int x, int y, int w, int h) {
        super(x, y, w, h);
        this.addUnit();
    }

    private void addUnit(){
        buttonReset.setHandler("left released", GameManager::reset);
        this.add(buttonReset);
        buttonExit.setHandler("left released", ()->System.exit(0));
        this.add(buttonExit);

        setSizeText.setText("Sweeper Size:");
        this.add(setSizeText);
        setSizeNumber.setForeground(StyleOption.LABEL_NUMBER);
        this.add(setSizeNumber);
        setBomberText.setText("Bomber Percent(%):");
        this.add(setBomberText);
        setBomberNumber.setForeground(StyleOption.LABEL_NUMBER);
        this.add(setBomberNumber);
        this.refresh();

        setSizeArrowLeft.setHandler("left pressed", ()->{
            GameManager.minusSweeperSize();this.refresh();
        });
        this.add(setSizeArrowLeft);
        setSizeArrowRight.setHandler("left pressed", ()->{
            GameManager.addSweeperSize();this.refresh();
        });
        this.add(setSizeArrowRight);
        setBomberArrowLeft.setHandler("left pressed", ()->{
            GameManager.minusBomberPercent();this.refresh();
        });
        this.add(setBomberArrowLeft);
        setBomberArrowRight.setHandler("left pressed", ()->{
            GameManager.addBomberPercent();this.refresh();
        });
        this.add(setBomberArrowRight);

        this.add(flagCount);

        this.add(timeCount);

        titleText.setText("HexaSweeper");
        titleText.setFont(new Font(StyleOption.LABEL_FONT, Font.BOLD, 48));
        this.add(titleText);
        versionText.setText(GameManager.VERSION + "   Created by " + GameManager.CREATOR);
        this.add(versionText);

    }

    public void refresh(){
        setSizeNumber.setText("" + GameManager.getSweeperSize());
        setBomberNumber.setText("" + GameManager.getBomberPercent());
    }

    @Override
    public void update() {
        super.update();
        if(flagCount != null)
            flagCount.setText(this.flagCountText());
        if(timeCount != null)
            timeCount.setText("Time(s): " + GameManager.$gameTime.getTimeText());
    }

    private String flagCountText(){
        switch (GameManager.getGameStatus()){
            case GameManager.GAME_RESET : return "Game Reset";
            case GameManager.GAME_START : return GameManager.isGamePause() ? "Game Pause" : "Bomber: " + GameManager.flagCount;
            case GameManager.GAME_FAILED : return "Game failed";
            case GameManager.GAME_SUCCEED : return "Game Succeed";
            default: return "Warning: Game Status undefined";
        }
    }
}
